Welcome back.
This lecture is dedicated towards learning all about the table data type.
Tables are super powerful and awesome because you can do so much with them.
You can store a bunch of information in an organized manner.
You can access information in an organized manner.
You can assign data a key so you can reference it instantly, just like we do for variables and much
more.
However, before we start learning how to use tables, we first have to understand what exactly a table
is.
A table in Lua is a container of values in your code, just as the scripts in our game are containers
for Lua code.
A table is a container that holds values of many different data types.
Tables are important because they allow us to organize and structure our data in a way that can be iterated
over.
What do I mean by iterate?
Well, iteration is the repetitive execution of the same code over and over.
If our tables did not store data in a predictable and organized manner, we would be unable to use the
same code over and over again to iterate or look through all the data inside of the table.
This is why tables are such a big deal.
They provide us a tool to meaningfully organize our data.
So let's go ahead and get started creating some tables.
What I'm going to do is I'm going to create a variable here.
I'm just going to call this example table.
And then to create a table.
As you remember earlier we use curly brackets.
So I'm going to put my first one here.
And then the second one automatically gets filled in for me already.
And now we have an empty table or an empty container.
There's no values stored inside of our table here.
However, I can start filling it up with some values.
For example, I can put the number ten here, and then to designate another value I'm going to put a
comma, and then I can pass something like 35.
If I want to store another value again we'll put another comma and we could do two.
We could do something like 187 or a -33.
And now here we have a table with five different values stored inside of it.
This table right here is known as an array.
Now an array is a data structure where values are stored chronologically.
This means that the first value stored in the table will always be the first, and the last value stored
inside of the table will always be the last.
Each one of these values are assigned something called an index.
As you remember in a string, each of the characters had a different index.
Well, in tables it works the same way.
Our first value has the index of one, our second value has the index of two, three, and so on.
Through all the values in our table.
This means we can access a specific value in this table by using the index.
So what I'm going to do is I'm going to use my print function.
And what we're going to do is we're going to pass the example table.
And then to index this table we put a bracket.
And then we can pass the index or the number.
In this case we can pass one here.
And this means I'm going to be grabbing the value that is stored at the first index inside of our table
or our array.
In this case that would be the value of ten.
What I can also do is I can grab the last value inside of this table as well.
And if you do not know the length of your table, we can get the length easily by again using that hashtag
operator.
This also works for tables.
And then we can pass example table.
And this will return a number that represents the length or how many values are stored inside of this
array.
So we have 12345 values.
And that means this will return the number five.
So if we go ahead and go to our test tab and hit run.
As you can see, the first value we get at the first index is ten, and then the value at the last index,
which was five, is -33.
Just like that, we have access or index values inside of our table.
Now indexing a table is great and all, but how can we insert and remove elements from our table during
the runtime of our script, or during the execution of our game?
Well, this is where the table library comes into play.
That's right.
We have another library inside of the Lua programming language to access the table library.
It's easy.
All we need to do is type out table and then we can index it.
And here we have a bunch of different functions for manipulating tables.
As you can see we can insert values into a table.
And we can also remove values from table.
We can clone tables.
We can clear or remove all of the data inside of a table, and a whole bunch of other neat stuff.
If you're more curious about all the functions available in the table library, there are plenty of
articles and documentation detailing the functionality of each one.
I'll leave a resource in this lecture detailing the other functions available in the table library.
The first function I would like to demonstrate is going to be the insert function or inserting a new
value inside of the table.
As you can see, this function needs to be passed a table and then a value to be inserted into that
table.
So what I'm going to do is I'm going to create a variable.
I'm going to call it new num.
And we can store a number like 20.
And then what I want to do is I want to pass my example table.
And I would like to insert this value of 20 inside of our table.
And what this is going to do is it's going to insert this value at the very last index.
Right.
Because whatever is first is first, and whatever is last is last.
And when we insert objects into our array, it's going to insert them chronologically.
It's not going to shove this number randomly between these other values.
Nope.
It's always going to go at the very end because that's how an array works.
So now that means we can print out the entirety of our table, and we should be able to see this new
value of 20 at the end of our table.
So if we hit run here.
Here's our table.
We can hit this arrow to fully see all the values inside of it.
And as you can see, the value of 20 is now stored at the sixth index in our table.
Here we have ten at the first 35 at the second, two at the third one, 87 at the fourth, and then
-33 at the fifth.
We can also remove a value out of our table using the table remove function, and this function needs
to be passed a table, and then it needs to be passed a number which represents the index where we would
like a value to be removed.
So for example, if I wanted to remove the first value inside of the table, I would pass my table.
And then I would pass one to denote that I would like to remove the value at the first index.
So that means our value of ten is going to be removed out of our table.
And if we go ahead and print example table back into the console, as you can see when we hit run.
And we go ahead and open this up.
Our ten is no longer there.
Previously, as you can see up here, the number ten was there, but now the number ten has disappeared.
Now, something important to note is that we do not need to store the return value from these different
functions back inside of our example table variable.
When we pass our table to these different functions, what's happening is we're actually passing a reference
to the table rather than a clone or copy of this table.
This means when this function performs some manipulation on the table, it's automatically applied to
the original values stored inside of our variable.
This means we do not have to do something like example table is equal to the return value.
This is the exact same thing as just doing this because it's automatically applied to our table.
If you do not wish to have it automatically applied to the table, this is where you would have to create
a clone of the table.
And that's where we would use the table clone function.
But before we get to that, another thing I would like to demonstrate is the table find function.
So for example here let's say I have a table that's super big and I know I have a value in there somewhere
I would like to remove, but I don't know what index or what position inside of my table that value
is at.
Well, what we could do is we could use the table dot find function.
And what this function does is it returns the value or the index of where that value is stored.
So I'm going to create a variable here I'm going to call it index.
And it's going to be equal to table dot find.
And I'm going to pass my example table.
And then I need to pass the value that I would like to find inside of our table.
And this instance it's considering our table the haystack.
And then it considers the value of the needle.
So we're finding the needle inside of the haystack or our table.
In this case maybe I want to remove the 20 we just inserted inside of our table.
So I can pass 20 here.
And then if it finds the value of 20 inside of our table, then it's going to return the index that
it's stored at.
This means I can use my table dot remove function, and then pass the index here to remove the value
of 20 out of our table.
So if we go ahead and hit run again.
And our first table we see 20 is inside of our table.
And then when it gets printed again, as you can see, 20 is no longer inside of our table.
It has been removed.
Before we move on.
I'm also going to demonstrate the tablecloth function.
So what I'm going to do is I'm going to create another variable.
I'm going to call this variable clone.
And it's going to be equal to table dot clone.
And all we need to pass to this is the table itself.
So we'll pass example table.
And now inside of this variable we have stored a copy or a new table that is a direct clone of this
table right here.
So what's stored in this variable is completely different to what's stored inside of this variable.
They are not the same.
This means if I were to use something like the table dot clear function to remove all of the data inside
of our table, and I pass clone here, what's going to happen is clone is going to be completely empty.
If I print clone into the console, we're just going to get an empty table.
However, if I print out our example table, this table here should still be filled with all of the
values that we've originally placed inside of it.
So if we hit run.
Here we get our empty table.
We removed all the values inside of it, but as you can see, our original table has remained unaffected,
right?
This is because we've created a clone of our original table and cleared all the values inside of it,
but our original table remains unaffected.
So you always need to use the table dot clone function when you want a clone of the table and manipulate
only that particular clone.
If I had a variable here and I instead I just stored the value that's inside of example table.
This variable and this variable here are both referencing the exact same value.
That means even though I use table dot clear on this variable, it's going to affect both of these variables
because both variables are referring to the exact same table.
So if I hit run.
As you can see now I get two tables printed that are completely empty.
It's because we did not clone the table right here.
Instead, we've just referenced the exact same value and doing table dot clear on this variable did
the exact same thing to this variable here because again they're referencing the same table.
So that's a very important to understand and note.
Use the table dot clone function when you absolutely need to clone an effect a copy of a table.
All right.
Very important.
Now before we end this lecture I want to talk about the difference between dictionaries and arrays.
Now we know a table is a data type that can encompass multiple different forms of storing data.
The tables that we were messing with here earlier we know were arrays, right.
However, there is another data type that we can create called a dictionary.
And a dictionary is where you can assign a key to a value inside of your table.
Basically, you're like creating a variable inside of the table, and you can reference that variable
directly to instantly grab that value instead of having to mess with different numbers and stuff as
the index.
However, a big difference between dictionaries and arrays is that dictionaries are not ordered or organized.
This means that order is not maintained.
So if you were to ever iterate through a dictionary, which we're going to do later in the course,
the first element is not guaranteed to actually be the first value stored inside of the table.
So to create a dictionary, we do the exact same thing.
We just put two curly brackets.
I'm going to store this inside of a variable.
I'm going to call it dictionary example.
And what I can do here is I can define a key and a value to be stored with that key.
It's called a key value pair.
It's just as the name suggests you have a key.
You got a value.
And they're a pair together.
Right.
Very simple.
So as an example here I'm going to create a key.
And I'm just going to call this key one.
And at key one I would like to store maybe the value of hello right a string.
So here we have our key.
And we have a value.
And they are a pair together.
If I would like to store another key value pair inside of our dictionary, we just put a comma to denote
to the interpreter.
Hey we would like to create another key value pair.
So I'm just going to copy this here.
And this time I'm going to call this key two.
And maybe inside of key two I would like to store the Word of World.
So now here we have our dictionary.
We have key one which stores hello.
And then we have key two which stores world.
And that means we can directly access these different values by using their keys.
So what we can do is we can reference our dictionary example variable.
And then to access these keys we can directly index the table by using the dot operator.
And here you can see our two keys.
We have key one and key two.
So if I did that this means I'm accessing the value stored in key one which is hello.
Or if I did key two I am now accessing the value in key two which is world.
And we can also do the exact same thing by using a string.
And here we are.
We have key one and key two because these different keys are automatically stored as strings.
So if I print this out into the console we should get hello printed right here.
And if I do print again and then refer to dictionary example and I index it and get key number two,
we should get world printed out in the console right here as well.
So let's go ahead and hit run.
And there we go.
We get hello, which is stored in that first key.
And then we have world stored in that second key.
Perfect.
If I would like to insert or remove values out of our dictionary, it's much easier to do.
We don't have to use the insert or remove function instead.
What I could do is get my dictionary example.
I can access, for example, key one and I can overwrite the value inside of there using the assignment
operator.
And maybe I want to replace the word with like howdy right.
So now key one the value inside of there has been overridden with this new string.
And that means if I print out dictionary example dot key one we should get howdy printed here instead.
So if we hit run.
There we go.
We get Hello world and then.
Howdy.
Right.
We've replaced the string of hello with the string of.
Howdy.
If I would like to remove a value out of my dictionary, then what I would do.
Is referred to dictionary example.
Again, get key one.
And inside of here maybe I want to completely remove it.
So I just set the value stored inside of there to nil.
And this is how we remove key value pairs within our dictionaries.
So now if I go and print out the entirety of my dictionary example, we should only have key two left
inside of the table.
So if I hit run.
Here we go.
We get Hello World.
We replaced it with howdy.
And then here is our table.
And as you can see, only key two remains because key one was assigned the value of nil, which automatically
removes it from our dictionary.
All righty.
This was a lot of information to digest, but I would like for you to let this information marinate
in your brain for a little bit.
Tables are the most powerful tool you have in Lua, and you will be using tables a lot in your scripts,
so it's crucial you understand how to create and manipulate tables.
Keep up the good work and I will see you in the next lecture.